package com.czy.owner.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.RxBusEvent;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.accountbalance.RechargeActivity;
import com.czy.owner.ui.order.BackPaidSuccessActivity;
import com.czy.owner.ui.order.OrderActivity;
import com.czy.owner.ui.order.OrderPaidActivity;
import com.czy.owner.ui.packagecardproject.BuyStorePackageActivity;
import com.czy.owner.ui.store.PaidActivity;
import com.czy.owner.ui.workorder.PaidEndOfPaymentActivity;
import com.czy.owner.ui.workorder.PaidSubscriptionActivity;
import com.czy.owner.ui.workorder.SubscribeOrdelActivity;
import com.czy.owner.utils.PhoneUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by wangyang on 2016/11/24adb.
 * 微信支付返回结果处理
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    @BindView(R.id.tv_paid_status)
    TextView tvPaidStatus;
    public static final String APP_ID = "wx23c8d286958d69ca";
    private IWXAPI api;
    private String result;
    private static String mRxBusEvent = "";


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_wx_pay_result;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.record_detail);
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void updateViews() {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            int errCode = resp.errCode;
            if (errCode == 0) {
                result = "支付成功";
                // imgPaidStatus.setBackgroundResource(R.mipmap.icon_wx_success);
                tvPaidStatus.setText(result);
                updataUserInfo();
            } else if (errCode == -1) {
                result = "支付失败";
                // imgPaidStatus.setBackgroundResource(R.mipmap.icon_wx_fail);
                tvPaidStatus.setText(result);
                PhoneUtils.ShowToastMessage(WXPayEntryActivity.this, result);
                ErrorPayment();
                finish();

            } else if (errCode == -2) {
                result = "取消支付";
                // imgPaidStatus.setBackgroundResource(R.mipmap.icon_wx_fail);
                tvPaidStatus.setText(result);
                PhoneUtils.ShowToastMessage(WXPayEntryActivity.this, result);
                CanclePayment();
                finish();

            }

        }
    }

    @OnClick(R.id.btn_wx_paid_complete)
    public void wxCompleteOnClick(View view) {
        finish();
    }

    public void ErrorPayment() {
        //支付尾款失败
        if (mRxBusEvent.equals(RxBusEvent.PAID_SECCESS_ENDOF)) {
            CommonEvent commonEvent = new CommonEvent(RxBusEvent.PAID_SECCESS_ENDOF);
            commonEvent.setType("error");
            RxBus.getDefault().post(Constants.PAID_SUCCESS_ENDOF_PAYMENT, commonEvent);
        }
    }

    public void CanclePayment() {
        //支付尾款取消
        if (mRxBusEvent.equals(RxBusEvent.PAID_SECCESS_ENDOF)) {
            CommonEvent commonEvent = new CommonEvent(RxBusEvent.PAID_SECCESS_ENDOF);
            commonEvent.setType("cancle");
            RxBus.getDefault().post(Constants.PAID_SUCCESS_ENDOF_PAYMENT, commonEvent);
        }
    }

    public static void wxPayPaidStart(Activity activity, String data, String type) {
        mRxBusEvent = type;
        IWXAPI msgApi = WXAPIFactory.createWXAPI(activity, APP_ID);
        PayReq req = new PayReq();
        String packageX = JsonUtil.getJsonValuesString(data, "package");
        String appid = JsonUtil.getJsonValuesString(data, "appid");
        String sign = JsonUtil.getJsonValuesString(data, "sign");
        String partnerid = JsonUtil.getJsonValuesString(data, "partnerid");
        String prepayid = JsonUtil.getJsonValuesString(data, "prepayid");
        String noncestr = JsonUtil.getJsonValuesString(data, "noncestr");
        String timestamp = JsonUtil.getJsonValuesString(data, "timestamp");

        req.packageValue = packageX;
        req.appId = appid;
        req.sign = sign;
        req.partnerId = partnerid;
        req.prepayId = prepayid;
        req.nonceStr = noncestr;
        req.timeStamp = timestamp;

        msgApi.registerApp(WXPayEntryActivity.APP_ID);
        msgApi.sendReq(req);
    }

    private void updataUserInfo() {
        //充值成功
        if (mRxBusEvent.equals(RxBusEvent.RECHARGE)) {
            RxBus.getDefault().post(Constants.RECHARGE_SUCCESS_CODE, new CommonEvent(RxBusEvent.RECHARGE));
        }
        //购买套餐
        if (mRxBusEvent.equals(RxBusEvent.BUG_PACKAGE)) {
            RxBus.getDefault().post(Constants.BUY_PACKAGE_SUCCESS_CODE, new CommonEvent(RxBusEvent.BUG_PACKAGE));
        }


        //门店付款成功
        if (mRxBusEvent.equals(RxBusEvent.PAID_SECCESS)) {
            RxBus.getDefault().post(Constants.PAYMENT_SECCESS, new CommonEvent(RxBusEvent.PAID_SECCESS));
        }

        //订单付款成功
        if (mRxBusEvent.equals(RxBusEvent.ORDER_PAID_SECCESS)) {

            Intent ti = new Intent(WXPayEntryActivity.this, BackPaidSuccessActivity.class);
            startActivity(ti);

//            Intent intent = new Intent();
//            intent.setAction(OrderActivity.orderAction);
//            intent.putExtra("currentId", 0);
//            sendBroadcast(intent);
            RxBus.getDefault().post(Constants.ORDER_PAYMENT_SECCESS_CODE, new CommonEvent(RxBusEvent.ORDER_PAID_SECCESS));
        }
        //支付定金成功
        if (mRxBusEvent.equals(RxBusEvent.ORDER_PAID_SECCESS_PAYMENT)) {
//            Intent intent = new Intent();
//            intent.setAction(SubscribeOrdelActivity.orderAction);
//            if (SubscribeOrdelActivity.currentId() == 0) {
//                intent.putExtra("currentId", 0);
//            } else if (SubscribeOrdelActivity.currentId() == 1) {
//                intent.putExtra("currentId", 2);
//            }
//            sendBroadcast(intent);
            RxBus.getDefault().post(Constants.ORDER_PAID_SUCCESS, new CommonEvent(RxBusEvent.ORDER_PAID_SECCESS_PAYMENT));
        }


        //支付尾款成功
        if (mRxBusEvent.equals(RxBusEvent.PAID_SECCESS_ENDOF)) {
//            Intent intent = new Intent();
//            intent.setAction(SubscribeOrdelActivity.orderAction);
//            if (SubscribeOrdelActivity.currentId() == 0) {
//                intent.putExtra("currentId", 0);
//            } else if (SubscribeOrdelActivity.currentId() == 3) {
//                intent.putExtra("currentId", 4);
//            }
//            sendBroadcast(intent);
            RxBus.getDefault().post(Constants.PAID_SUCCESS_ENDOF_PAYMENT, new CommonEvent(RxBusEvent.PAID_SECCESS_ENDOF));
        }


        finish();
    }


}